package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.TEquipment;
import com.example.model.entity.TEquipmentPart;
import com.example.model.entity.TPlan;
import com.example.model.entity.TSource;
import com.example.model.query.TEquipmentQuery;
import com.example.model.query.TPlanQuery;
import com.example.model.query.TSourceQuery;
import com.example.service.TEquipmentPartService;
import com.example.service.TEquipmentService;
import com.example.service.TPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * 告警预案表
 * @author zhangr
 * @Description Created on 2020-12-17
 */

@RestController
@Controller
@RequestMapping("tPlan")
@Api(value = "告警预案管理",tags={"告警预案管理接口"})
public class TPlanController {
    @Autowired
    private TPlanService planService;

    @Autowired
    private TEquipmentPartService equipmentPartService;

    @Autowired
    private TEquipmentService equipmentService;

    /**
     * 分页查询列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "分页", httpMethod = "GET")
    public ApiResult<TPlanQuery> pageByQuery(TPlanQuery query) {
        try {
            return planService.pageUserVoByQuery(query);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
        }
    }

    /**
     * 获取某通道所有预案
     * @return
     */
    @ApiOperation(value = "获取某通道通道预案", notes = "查询获取某通道所有预案", httpMethod = "GET")
    @GetMapping("/getList/{id}")
    public ApiResult<List<TPlan>> getList(@PathVariable("id") String id){
        try {
            return planService.getList(id);
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }

    /**
     * 获取预案下拉框
     * @return
     */
    @ApiOperation(value = "获取通道预案", notes = "查询获取所有预案", httpMethod = "GET")
    @GetMapping("/getList")
    public ApiResult<List<TPlan>> getList(){
        try {
            return planService.getList();
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }

    /*
     * 新增
     *
     */
    @ApiOperation(value = "新增预案", notes = "新增预案", httpMethod = "POST")
    @PostMapping("/save")
    public ApiResult<Boolean> save(@RequestBody TPlan plan){
        try{
            return planService.saveObj(plan);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 修改
     *
     */
    @ApiOperation(value = "修改预案", notes = "修改预案", httpMethod = "POST")
    @PostMapping("/modify")
    public ApiResult<Boolean> modify(@RequestBody TPlan plan){
        try{
            QueryWrapper<TPlan> planQueryWrapper = new QueryWrapper<>();
            planQueryWrapper.lambda().eq(TPlan::getIsValid,1L)
                    .eq(TPlan::getPlanNum,plan.getPlanNum())
                    .ne(TPlan::getPlanId,plan.getPlanId());
            int num = planService.count(planQueryWrapper);
            if(num > 0){
                return new ApiResult<>(-1,"预案编号已被占用");
            }
            return new ApiResult<>(planService.updateById(plan));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 删除
     *
     */
    @GetMapping("/del/{id}")
    public ApiResult<Boolean> del(@PathVariable("id") String id){
        try {
            QueryWrapper<TPlan> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TPlan::getPlanId,id);
            TPlan obj =new TPlan();
            obj.setIsValid(0L);
            return new ApiResult<>(planService.update(obj,queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }

    /**
     * 预案主动触发
     * @param partId 通道id
     * @param warningId 告警id
     * @return
     */
    @ApiOperation(value = "预案主动触发", notes = "预案主动触发", httpMethod = "GET")
    @GetMapping("/startPlan/{partId}/{warningId}")
    public ApiResult<Boolean> startPlan(@PathVariable("partId") String partId,@PathVariable("warningId") String warningId){
        try {
            if(StringUtils.isBlank(partId) || StringUtils.isBlank(warningId)){
                return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ACITIVITY_EXPORT_FALSE);
            }
            TEquipmentPart part = equipmentPartService.getById(partId);
            if(part == null || part.getIsValid()==0L){
                return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_EXPORT_NULL);
            }
            return equipmentService.startPlan(part,warningId);
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_EXPORT_FALSE);
        }
    }


}

